package com.soup.view;

import com.soup.domain.Person;
import com.soup.view.component.Context;
import com.soup.view.component.MeFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class RootFrame extends MeFrame {


    private JButton rsg = new JButton("新办证件");
    private JButton update = new JButton("证件修改");
    private JButton del = new JButton("证件注销");
    private JButton replace = new JButton("排行榜");
    private JButton append = new JButton("新书入库");
    private JButton index = new JButton("图书检索");


    private Context right = null;

    private MeControl meControl = null;

    public RootFrame(Login login, Person person) {
        super("管理员" , 1000, 600);

        meControl = new MeControl(login,this);

        meControl.setName(person.getName());
        meControl.setState(person.getState());

        meControl.setLocation(850,100);
        context.add(meControl);

        //初始化左边
        context.add(rsg,20);
        rsg.setBounds(40,60,120,50);

        context.add(update,20);
        update.setBounds(40,130,120,50);

        context.add(del,20);
        del.setBounds(40,200,120,50);

        context.add(replace,20);
        replace.setBounds(40,270,120,50);

        context.add(append,20);
        append.setBounds(40,340,120,50);

        context.add(index,20);
        index.setBounds(40,410,120,50);


        // 左边添加监听器
        rsg.addActionListener(this);
        update.addActionListener(this);
        replace.addActionListener(this);
        del.addActionListener(this);
        append.addActionListener(this);
        index.addActionListener(this);
    }

    @Override
    public void doActionPerformed(ActionEvent e) {
        if (right != null){
            context.remove(right);
        }
        if (e.getSource() == rsg){
            right = new PersonForm(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == update){
            right = new UpdateFrame(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == del){
            right = new DeleteFrame(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == append){
            right = new BookFrame(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == index){
            right = new SearchBookFrame(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }else if (e.getSource() == replace){
            right = new TopFrame(this.getWidth()-400,this.getHeight()-100);
            context.add(right);
            right.setLocation(200,60);
        }

    }
}